<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function isSelected($val1, $val2) {
	if ($val1 == $val2)
		return ' selected="selected"';
	else
		return '';
}


function dateButtons($id, $d) {
	$ret = "";
	$ts = strtotime($d);
	// N (day of the week)  Fr-5 Sa-6 So-7
	$day = date("N", $ts);

	// generate dates
	if ($day == 5) {
		$fr = $ts;
	} 
	else if ($day == 6) {
		$fr = add_days($ts, -1);
	}
	else if ($day == 7) {
		$fr = add_days($ts, -2);
	}
	else {
		return "Englische Woche?";
	}
	$sa = add_days($fr, 1);
	$so = add_days($fr, 2);

	// Freitag 20:30
	$ret .= _print_button($d, $id, $fr, "20:30:00", "Fr20");
	// Samstag 15:30
	$ret .= _print_button($d, $id, $sa, "15:30:00", "Sa15");
	// Samstag 18:30
	$ret .= _print_button($d, $id, $sa, "18:30:00", "Sa18");
	// Sonntag 15:30
	$ret .= _print_button($d, $id, $so, "15:30:00", "So15");
	// Sonntag 17:30
	$ret .= _print_button($d, $id, $so, "17:30:00", "So17");
	return $ret;
}

function _print_button($d, $id, $ts, $time, $label) {
	
	$btndate = date("Y-m-d", $ts)." ".$time;
	
	$tmpl = '<button type="button" class="btn_%s%s" rel="%s">%s</button>';
	if ($d == $btndate) $active=' active'; else $active = '';
	return sprintf($tmpl, $id, $active, $btndate, $label);
}

function add_days($cd, $days = 0) {
	$day = date('d', $cd) + $days;
	
	$ts = mktime(
			date('h', $cd), date('i', $cd), date('s', $cd),
			date('m', $cd), $day, date('Y', $cd));

	return $ts;
}


function formatDate($date) {
	return date("d.m.y H:i", strtotime($date) );
}

function getPoints($eH, $eA, $tH, $tA) {
	$CI =& get_instance();
	$points_perfect    = $CI->config->item('points_perfect');
	$points_difference = $CI->config->item('points_difference');
	$points_tendency   = $CI->config->item('points_tendency');
	
	if (strlen($eH)==0 || strlen($tH)==0 || strlen($eA)==0 || strlen($tA)==0)
		return "";
	
	$isPerfect = ($eH == $tH) && ($eA == $tA );
	$isDiff = (($tH-$tA) ==  ($eH-$eA));
	$isTendency = (($tH < $tA && $eH < $eA) || ($tH > $tA && $eH > $eA));

	$points = 0;
	if ($isPerfect)
		$points = $points_perfect;
	else if($isDiff)
		$points = $points_difference;
	else if($isTendency)
		$points = $points_tendency;

	$balls = "";
	$props = array('src' => 'resources/images/ball-small.png', 'alt' => 'BALL', 'class' => 'pointballs');
	for ($i=0;$i<$points;$i++)
		echo img($props);
	 
	return $points;
}

function getCssClass($name, $prefix = '') {
	// $name = strtolower($name);
	$s = array(" ", ".", "ö","ä","ü","Ä","Ö","Ü","ß");
	$r = array("_", "" , "oe","ae","ue","Ae","Oe","Ue","ss");	
	$name = $prefix.str_replace($s, $r, $name);
	return $name;
}



function generateUserSettings($url, $mtype, $mvalue) {
	$settings = null;

	if (strlen($url) > 0)
		$settings->url = $url;

	if (strlen($mvalue) > 0) {
		$m = null;
		$m->type = $mtype;
		$m->value = $mvalue;
		$settings->messenger = $m;
	}
	
	return $settings;
}
